<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Admin\Picture;
use Intervention\Image\ImageManagerStatic as Image;

class PictureController extends Controller
{
    //
    public function index(){
        $data = Picture::get();

    	return view('admin.picture.index',compact('data'));
    }

    public function picadd(Request $request){

    	 if ($request->isMethod('post')) {
            $result = Picture::insert([
          
                'title'  => $request->input('title'),             
                'link'   => $request->input('link'),
                'sort'	 => $request->input('sort'),
                'photo'  => $request->input('photo'),
                      
            ]);
            return $result ? '1' : '0';
        }else{

        	return view('admin.picture.picadd');

        }




    }

    public function uploadpic(Request $request){


        $path = 'storage/flash/';
 
     if ($request->hasFile('file') && $request->file('file')->isValid()) {
        
        $photo = $request->file('file');
        //var_dump($photo);
        $file_name = uniqid().'.'.$photo->getClientOriginalExtension();
        $file_path =public_path($path);
        $thumbnail_file_path = $file_path .$file_name;
        $image = Image::make($photo)->save($thumbnail_file_path);
        //echo '上传成功';resize(700, 640, function ($constraint) {$constraint->aspectRatio();})->
  
        $result = [ 
            'errCode'  => '0',
            'errMsg'   => '',
            'succMsg'  => '文件上传成功！',
            'path'     => '/'.$path.$file_name,
            
         
        ];
    
          
        }
      
        else {
            $result = [
                'errCode'  => '0000001',
                'errMsg'   => $request ->file('file')->getErrorMessage(),

            ];
           
        }
        return response()-> json($result);
 	
          
     
    }

}
